package demo3;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wx
 * Date: 2024-05-04
 * Time: 18:10
 */
//无返回值无参数
@FunctionalInterface
interface NoParameterNoReturn {
    void test();
}

//无返回值一个参数
@FunctionalInterface
interface OneParameterNoReturn {
    void test(int a);
}

//无返回值多个参数
@FunctionalInterface
interface MoreParameterNoReturn {
    void test(int a, int b);
}

//有返回值无参数
@FunctionalInterface
interface NoParameterReturn {
    int test();
}

//有返回值一个参数
@FunctionalInterface
interface OneParameterReturn {
    int test(int a);
}

//有返回值多参数
@FunctionalInterface
interface MoreParameterReturn {
    int test(int a, int b);
}

//lambda 变量捕获
//在匿名内部类当中,在使用类外变量的时候,
// 这个变量要么是常量,要么是不能修改的变量
// lambda 表达式中一样.

public class Test {
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1, "hello");
        map.put(2, "bit");
        map.put(3, "hello");
        map.put(4, "lambda");

        map.forEach(new BiConsumer<Integer, String>() {
            @Override
            public void accept(Integer integer, String s) {
                System.out.println("key" + integer + "value" +s);
            }
        });
        System.out.println("======================================================");
        map.forEach((key,val) ->System.out.println("key" + key + "value" + val));
    }

    public static void main5(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Hello");
        list.add("bit");
        list.add("hello");
        list.add("lambda");
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String str) {
                //简单遍历集合中的元素。
                System.out.print(str + " ");
            }
        });
        list.forEach(s -> System.out.println(s + " "));
        System.out.println("=====================================");
        list.sort(new Comparator<String>() {
            @Override
            public int compare(String s, String t1) {
                return s.compareTo(t1);
            }
        });
        System.out.println("=====================================");
        list.sort((o1, o2) -> o1.compareTo(o2));
        System.out.println("=====================================");

    }

    public static void main3(String[] args) {
//        NoParameterReturn noParameterReturn = () -> {
//            return 10;
//        };
        NoParameterReturn noParameterReturn = () -> 10;
        System.out.println(noParameterReturn.test());

//        OneParameterReturn oneParameterReturn = (a) -> {
//            return a = a+10;
//        };
        OneParameterReturn oneParameterReturn = a -> a + 10;
        System.out.println(oneParameterReturn.test(1));
//
//        MoreParameterReturn moreParameterReturn = (a, b) -> {
//            return a + b;
//        };
        MoreParameterReturn moreParameterReturn = (a, b) -> a + b;
        System.out.println(moreParameterReturn.test(1, 3));
    }

    //可读性差
    public static void main2(String[] args) {
        NoParameterNoReturn noParameterNoReturn = () -> System.out.println("重写text方法 ");
        noParameterNoReturn.test();

        OneParameterNoReturn oneParameterNoReturn = (a) -> {
            System.out.println("oneParameterNoReturn");
        };
        oneParameterNoReturn.test(10);

        MoreParameterNoReturn moreParameterNoReturn = (int a, int b) -> {
            System.out.println(a + b);
        };

    }

    public static void main1(String[] args) {
        // 匿名内部类
        NoParameterNoReturn noParameterNoReturn = new NoParameterNoReturn() {
            @Override
            public void test() {
                System.out.println("重写text方法 ");
            }
        };
        noParameterNoReturn.test();
    }
}
